package com.onesports.intelligent.k12.polarlight.config;

import com.onesports.intelligent.k12.polarlight.constant.ProfileConstants;
import org.springframework.core.env.Environment;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

public class CustomTyperFilter implements TypeFilter {
    private ClassMetadata classMetadata;

    private final Environment env;

    private final static String WX_API_PATH = "com.onesports.intelligent.k12.polarlight.controller.wx";
//    private final static String WX_ORGANIZATION_API_PATH = "com.onesports.intelligent.k12.polarlight.controller.wx";
    private final static String OPERATION_API_PATH = "com.onesports.intelligent.k12.polarlight.controller.operation";
    private final static String ORGANIZATION_API_PATH = "com.onesports.intelligent.k12.polarlight.controller.organization";
    private final static String SCHEDULE_API_PATH = "com.onesports.intelligent.k12.polarlight.schedule";

    public CustomTyperFilter(Environment env) {
        this.env = env;
    }

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
//        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
        //当前当前扫描的类信息
        this.classMetadata = metadataReader.getClassMetadata();
//      当前类资源（类的路径）
//        Resource resource = metadataReader.getResource();
        String className = classMetadata.getClassName();
        if (activeProfiles.contains(ProfileConstants.SPRING_PROFILE_WX)) {
            return className.startsWith(OPERATION_API_PATH)
                    || className.startsWith(ORGANIZATION_API_PATH)
//                    || className.startsWith(WX_ORGANIZATION_API_PATH)
                    || className.startsWith(SCHEDULE_API_PATH);
        } else {
            return className.startsWith(WX_API_PATH);
        }
    }
}
